package com.code;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obThuChi;

import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TK_ThuChi extends Activity {
	LinearLayout llMain;
	private final int REQUEST_THUCHI = 100;
	private StringBuffer QUERY_STORE_THUCHI;
	private int chooseThuChi;
	private String  strKhoChon = "";
	ArrayList<Integer> date_actual;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tk_thuchi);

		chooseThuChi = super.getIntent().getExtras().getInt("choose_mau");
//		strNhomChon = super.getIntent().getExtras().getString("strNhomChon");
		strKhoChon = super.getIntent().getExtras().getString("strKhoChon");
		date_actual = super.getIntent().getExtras().getIntegerArrayList("date_actual");
		String date = String.valueOf(date_actual.get(2))+DetailCuocGoi.setMonth(date_actual.get(1))+Global.formatDay(date_actual.get(0));

		if (chooseThuChi == TK_ChonMau.CHOOSE_THU) {
			QUERY_STORE_THUCHI = new StringBuffer(" SELECT Nguoithuchien, Diachi, SoTien, Vekhoan, Kho, ngay ");
			QUERY_STORE_THUCHI.append(" FROM Phieuthu ");
			QUERY_STORE_THUCHI.append(" WHERE ngay >= '"+date+"' AND ngay <= '"+date+"'");
		} else {
			QUERY_STORE_THUCHI = new StringBuffer(" SELECT Nguoithuchien, Diachi, SoTien, Vekhoan, Kho, ngay ");
			QUERY_STORE_THUCHI.append(" FROM Phieuchi ");
			QUERY_STORE_THUCHI.append(" WHERE ngay >= '"+date+"' AND ngay <= '"+date+"'");
		}
		if(!strKhoChon.equals(""))
		QUERY_STORE_THUCHI.append(" AND Kho COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"+strKhoChon+"', ','))");
		llMain = (LinearLayout) findViewById(R.id.llMain);
		
		selectAllDetailShipReceipt(QUERY_STORE_THUCHI.toString());
	}
	
	public Boolean selectAllDetailShipReceipt(String sql)
	{
		try{
			ResultSet rs = Global.excuteQuerySQLServer(sql);
			loadData(rs);
			rs.close();
//			conn.close();
			return true;
		}catch(Exception ex)
		{
			return false;
		}	
	}
	
	public void loadData(final ResultSet rs) throws SQLException
	{//load date edit by trangbm date 15/03		
		Float total = 0.0f;
		final TableLayout tL = new TableLayout(this);
		llMain.addView(tL);
	    TableRow tR = new TableRow(this);
		tR.setWeightSum(4.0f);
		
		TextView tvKhachHang = new TextView(this);
		tvKhachHang.setText("Khách hàng");
		TK_TongMatHang.setLayoutForTextView(tvKhachHang, true, Gravity.CENTER, 10,1.0f);
		
		TextView tvVaoKhoan = new TextView(this);
		tvVaoKhoan.setText("Về khoản");
		TK_TongMatHang.setLayoutForTextView(tvVaoKhoan, true, Gravity.CENTER, 10,2.5f);
		
		TextView tvSoTien = new TextView(this);
		tvSoTien.setText("Số tiền");
		TK_TongMatHang.setLayoutForTextView(tvSoTien, true, Gravity.CENTER, 10,0.5f,true);
		
		tR.addView(tvKhachHang);
		tR.addView(tvVaoKhoan);
		tR.addView(tvSoTien);
		tL.addView(tR);
		try {
			while(rs.next())
			{
				final obThuChi obThuChi = new obThuChi();
				if(rs.getString("Nguoithuchien") != null)
					obThuChi.setKhachHang(rs.getString("Nguoithuchien").toString());
				if(rs.getString("DiaChi") != null)
					obThuChi.setDiaChi(rs.getString("DiaChi").toString());
				if(rs.getString("SoTien") != null)
				{
					obThuChi.setSoTien(Global.formatDecimal(Float.valueOf(rs.getString("SoTien"))));
					total = total + Float.valueOf(rs.getString("SoTien"));
				}else				
					obThuChi.setSoTien("0");	
				if(rs.getString("Kho")!= null)
					obThuChi.setKho(rs.getString("Kho").toString());
				if(rs.getString("Vekhoan")!=null)
					obThuChi.setVaoKhoan(rs.getString("Vekhoan").toString());
				if(rs.getString("ngay") != null)
					obThuChi.setNgay(rs.getString("ngay"));
				TableRow tRow= new TableRow(this);	
				tRow.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						for (int i = 0; i < tL.getChildCount(); i++) {
							tL.getChildAt(i).setBackgroundColor(
									Color.TRANSPARENT);
						}
						v.setBackgroundColor(Color.YELLOW);
					}
				});
				tRow.setOnLongClickListener(new OnLongClickListener() {					
					public boolean onLongClick(View v) {						
						ArrayList<String> detail = new ArrayList<String>();
						detail.add(0, obThuChi.getKhachHang());
						detail.add(1, obThuChi.getKho());
						detail.add(2, obThuChi.getDiaChi());
						detail.add(3, Global.fomartDate(obThuChi.getNgay()));
						if(!obThuChi.getKhachHang().equals(""))
						{
							Intent i = new Intent(TK_ThuChi.this,DetailThuChi.class);
							Bundle b = new Bundle();
							b.putStringArrayList("detailthuchi", detail);
							i.putExtras(b);
							startActivityForResult(i, REQUEST_THUCHI);
						}
						return false;
					}
				});
				TextView tvKhachHangs = new TextView(this);
				tvKhachHangs.setText(obThuChi.getKhachHang());
				TK_TongMatHang.setLayoutForTextView(tvKhachHangs, false, Gravity.LEFT, 10,1.0f);
				
				TextView tvVaoKhoans = new TextView(this);
				tvVaoKhoans.setText(obThuChi.getVaoKhoan());
				TK_TongMatHang.setLayoutForTextView(tvVaoKhoans, false, Gravity.LEFT, 10,2.5f);
				
				TextView tvSoTiens = new TextView(this);
				tvSoTiens.setText(obThuChi.getSoTien());
				TK_TongMatHang.setLayoutForTextView(tvSoTiens, false, Gravity.RIGHT, 10,0.5f,true);
//				tvSoTiens.setGravity(Gravity.RIGHT);
				
				tRow.addView(tvKhachHangs);
				tRow.addView(tvVaoKhoans);
				tRow.addView(tvSoTiens);
				tL.addView(tRow);			
			}
			// end
			TableRow tREnd = new TableRow(this);
			tREnd.setWeightSum(4.0f);
			
			TextView tvEndKhachHang = new TextView(this);
			tvEndKhachHang.setText("");
			TK_TongMatHang.setLayoutForTextView(tvEndKhachHang, false, Gravity.CENTER, 10,1.0f);
			tvEndKhachHang.setBackgroundResource(R.layout.border_topleftbottom);
			
			TextView tvEndDiaChi = new TextView(this);
			tvEndDiaChi.setText("Tổng tiền");
			TK_TongMatHang.setLayoutForTextView(tvEndDiaChi, false, Gravity.CENTER, 10,2.5f);
			tvEndDiaChi.setBackgroundResource(R.layout.border_topleftbottom);
			
			TextView tvEndSoTien = new TextView(this);
			tvEndSoTien.setText(Global.formatDecimal(total).toString());
			TK_TongMatHang.setLayoutForTextView(tvEndSoTien, false, Gravity.RIGHT, 10,0.5f,true);
			tvEndSoTien.setBackgroundResource(R.layout.border);
//			tvEndSoTien.setGravity(Gravity.RIGHT);
			
			tREnd.addView(tvEndKhachHang);
			tREnd.addView(tvEndDiaChi);
			tREnd.addView(tvEndSoTien);
			tL.addView(tREnd);
		} catch (java.sql.SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
